# -*- coding:utf-8 -*-
# @File       :   二分进阶2.py
# @Author     :   LINING
# @Time       :   2024/3/4 16:09
# @Description : 有重复元素
# 求前驱
nums = [1,3]


def get_min(num, num_list):
    # 求前驱，最后一个小于的数
    left = -1
    right = len(num_list)-1
    while left < right:
        mid = int((left + right+1) / 2)
        if num_list[mid] >= num:
            right = mid-1  # -1的情况，只有两个数可能会死循环，所以mid要上取整
        else:
            left = mid
    return left


print(get_min(4, nums))
# 死循环情况
"""
nums = [1,3]
num = 4
left = -1
right = 1
mid = 0
num_mid = num[0]
4>1
left = 0
right=1
mid=0
"""